//
//  MJRefreshNomalHeader_CN.m
//  refesh
//
//  Created by macpro on 16/9/14.
//  Copyright © 2016年 macpro. All rights reserved.
//

#import "MJRefreshNomalHeader_CN.h"
#import "ABClockView.h"
@interface MJRefreshNomalHeader_CN()
{
    __unsafe_unretained UIImageView *_arrowView;
}
@property (weak, nonatomic) UIActivityIndicatorView *loadingView;
/**
 表视图
 */
@property(retain,nonatomic)ABClockView *clockView;
@end

@implementation MJRefreshNomalHeader_CN
#pragma mark - 懒加载子控件

-(ABClockView *)clockView
{
    if (!_clockView) {
        _clockView = [[ABClockView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        _clockView.backgroundColor = [UIColor clearColor];
        _clockView.realTime = YES;
        _clockView.currentTime = YES;
        //外框
        _clockView.borderWidth = 1;
        _clockView.borderColor =[UIColor yellowColor];
        //表盘
        _clockView.faceBackgroundColor = [UIColor clearColor];
        _clockView.faceBackgroundAlpha = 1;
        //刻度
        _clockView.graduationColor = [UIColor whiteColor];
        _clockView.graduationLength = 5;
        //指针
        //长
        _clockView.hourHandLength = 15;
        _clockView.minuteHandLength = 18;
        _clockView.secondHandLength = 40;
        //宽
        _clockView.hourHandWidth = 2;
        _clockView.minuteHandWidth = 1.5;
        _clockView.secondHandWidth = 1;
        
        //颜色
        _clockView.hourHandColor = [UIColor blackColor];
        _clockView.minuteHandColor = [UIColor blackColor];
        _clockView.secondHandColor = [UIColor redColor];
    
        [self addSubview:_clockView];
    }
    return _clockView;
    
}
- (UIImageView *)arrowView
{
    if (!_arrowView) {
        UIImage *image = [UIImage imageNamed:MJRefreshSrcName(@"arrow.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"arrow.png")];
        UIImageView *arrowView = [[UIImageView alloc] initWithImage:image];
        [self addSubview:_arrowView = arrowView];
    }
    return _arrowView;
}

- (UIActivityIndicatorView *)loadingView
{
    if (!_loadingView) {
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:self.activityIndicatorViewStyle];
        loadingView.hidesWhenStopped = YES;
        [self addSubview:_loadingView = loadingView];
    }
    return _loadingView;
}

#pragma mark - 公共方法
- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
{
    _activityIndicatorViewStyle = activityIndicatorViewStyle;
    
    self.loadingView = nil;
    [self setNeedsLayout];
}

#pragma makr - 重写父类的方法
- (void)prepare
{
    [super prepare];
    
    self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}

- (void)placeSubviews
{
    [super placeSubviews];
    
    // 箭头的中心点
    CGFloat arrowCenterX = self.mj_w * 0.5;
    if (!self.stateLabel.hidden) {
        arrowCenterX -= 100;
    }
    CGFloat arrowCenterY = self.mj_h * 0.5;
    CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
    
    
    // 表头
    if (self.clockView.constraints.count == 0) {
        self.clockView.mj_size = self.clockView.bounds.size;
        self.clockView.center = arrowCenter;
    }
    
    // 箭头
    if (self.arrowView.constraints.count == 0) {
        self.arrowView.mj_size = self.arrowView.image.size;
        self.arrowView.center = arrowCenter;
    }
    
    // 圈圈
    if (self.loadingView.constraints.count == 0) {
        self.loadingView.center = arrowCenter;
    }
}

-(void)setPullingPercent:(CGFloat)pullingPercent
{

    _clockView.maskViewProgress = [NSString stringWithFormat:@"%.2f",pullingPercent];
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        
        if (oldState == MJRefreshStateRefreshing) {
            self.arrowView.transform = CGAffineTransformIdentity;
            
            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                self.loadingView.alpha = 0.0;
            } completion:^(BOOL finished) {
                // 如果执行完动画发现不是idle状态，就直接返回，进入其他状态
                if (self.state != MJRefreshStateIdle) return;
                
                self.loadingView.alpha = 1.0;
                [self.loadingView stopAnimating];
                self.arrowView.hidden = NO;
            }];
        } else {
            [self.loadingView stopAnimating];
            self.arrowView.hidden = NO;
            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                self.arrowView.transform = CGAffineTransformIdentity;
            }];
        }
    } else if (state == MJRefreshStatePulling) {
        
        //下拉百分之百
        
        NSLog(@"statePulling");
        [self.loadingView stopAnimating];
        self.arrowView.hidden = NO;
        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
            self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
        }];
    } else if (state == MJRefreshStateRefreshing) {
        self.loadingView.alpha = 1.0; // 防止refreshing -> idle的动画完毕动作没有被执行
        [self.loadingView startAnimating];
        self.arrowView.hidden = YES;
    }
}
@end
